package tmproject.hlhj.fhp.cartrans.modules

import android.content.Context
import com.example.mymvp.BaseModule
import com.lzy.okgo.model.Response
import tmproject.hlhj.fhp.cartrans.convert.MyException
import tmproject.hlhj.fhp.cartrans.http.Apis
import tmproject.hlhj.fhp.cartrans.http.BaseObserver
import tmproject.hlhj.fhp.cartrans.http.LoadingAction
import tmproject.hlhj.fhp.cartrans.javaBean.BaseBean
import tmproject.hlhj.fhp.cartrans.javaBean.ExamListBean
import tmproject.hlhj.fhp.cartrans.utils.LoadingUtil

/**
 * Created by Never Fear   on 2018\8\28 0028.
Never More....
 */
class ExamModule:BaseModule {
    var listener:OnExamListener?=null

    fun getExams(c:Context,token:String?,page:Int,pageSize:Int){
        Apis.getExamList(token, page, pageSize)
                ?.let {
                    it.doOnSubscribe(LoadingAction(c,false))
                            .subscribe(object :BaseObserver<Response<ExamListBean>>(c){
                                override fun failed(e: Throwable?) {
                                }

                                override fun onError(e: Throwable) {
                                    LoadingUtil.hideLoading()
                                    if (e is MyException){
                                        if (e?.code==408){
                                            listener?.noMore()
                                        }
                                    }
                                }
                                override fun httpSuccess(t: Response<ExamListBean>) {
                                    listener?.gotExams(t.body())
                                }

                                override fun httpErroCode(baseBean: BaseBean) {
                                }
                            })
                }
    }
    interface OnExamListener{
        fun gotExams(examListBean: ExamListBean)

        fun noMore()
    }
}